﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BarCode.Common;
using System.IO;
using BarCode.Module;
using BarCode.AccessDao;
using System.Runtime.InteropServices;
using BarCode.Bean;

namespace BarCode.Forms
{
    public partial class SGBarCode : Form
    {
        private static readonly log4net.ILog LogUtil = log4net.LogManager.GetLogger(typeof(Base_BarCode));


        [DllImport("user32.dll")]
        public static extern bool MessageBeep(uint uType);

        private int workAmount;

        public SGBarCode()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Base_BarCode_Load(object sender, EventArgs e)
        {
            lblPreUser.Text = "";
            lblPreUser.ForeColor = Color.Blue;
            lblUser.Text = GlobalVal.CurrentUser.User;
            lblRepeat.Visible = false;

            // 获取当前用户当天的工作量
            DB01Dao db01 = new DB01Dao(GlobalVal.serverMysqlInfo.MYSQL_IP, GlobalVal.serverMysqlInfo.USERID, GlobalVal.serverMysqlInfo.USERPWD);
            workAmount = db01.getGasWorkAmount(GlobalVal.CurrentUser.User);
            lblAmount.Text = (workAmount + GlobalVal.BASE_COUNT).ToString();

            txtBarCode.Text = "";
        }

        /// <summary>
        /// 窗体关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Base_BarCode_FormClosed(object sender, FormClosedEventArgs e)
        {
            //Application.Exit();
        }


        /// <summary>
        /// 二维码输入后回车事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtBarCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if (e.KeyChar == (char)13)
                {
                    lblPreUser.ForeColor = Color.Blue;
                    string inputBarCode = txtBarCode.Text;
                    lblRepeat.Visible = false;
                    lblPreUser.Text = "";
                    // 查询信息是否存在
                    DB01Dao db01 = new DB01Dao(GlobalVal.serverMysqlInfo.MYSQL_IP, GlobalVal.serverMysqlInfo.USERID, GlobalVal.serverMysqlInfo.USERPWD);
                    // 查询前道工序是否存在
                    string preUser = db01.getTMZUser(inputBarCode);
                    if (string.IsNullOrEmpty(preUser))
                    {
                        lblPreUser.ForeColor = Color.Red;
                        lblPreUser.Text = "不存在";
                        ComUtil.sendFail(workAmount);
                        // 清空输入内容
                        txtBarCode.Text = "";
                        txtBarCode.Focus();

                    }
                    else
                    {
                        lblPreUser.ForeColor = Color.Blue;
                        lblPreUser.Text = preUser.ToString();
                        txtBarCode.Focus();
                        // 保存本道工序记录
                        GasCodeInfo gasInfo = new GasCodeInfo();
                        gasInfo.meters_no = inputBarCode;
                        gasInfo.operator_name = GlobalVal.CurrentUser.User;
                        gasInfo.Opt_date = DateTime.Now;
                        db01.updateSGCode(gasInfo);
                        // 获取当前用户当天的工作量
                        workAmount = db01.getGasWorkAmount(GlobalVal.CurrentUser.User);
                        lblAmount.Text = (workAmount + GlobalVal.BASE_COUNT).ToString();

                        // 清空输入内容
                        txtBarCode.Text = "";
                    }
                        
                    
                    
                }
            }
            catch (Exception ex)
            {
                LogUtil.Error(ex);
            }
        }

        /// <summary>
        /// 输入有误时弹框显示
        /// </summary>
        /// <param name="msg"></param>
        private void ShowInputWarn(string msg, uint beep)
        {
            txtBarCode.Text = "";
            txtBarCode.Focus();
           
            if (beep > 0)
            {
                MessageBeep(beep);
            }
            MessageUtil.Warn(msg);
        }

        private void txtBarCode_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
